package culosic.mdpocket.html;

import java.util.ArrayList;
import java.util.List;

import culosic.mdpocket.core.Node;
import culosic.mdpocket.core.*;

abstract class HtmlNode implements Node {

	private Node parent = null;
	private List<Node> childs = new ArrayList<>();

	// 转化、显示、生成
	public abstract void show(HtmlOut out);

	public void showChildren(HtmlOut out) {
		for (Node child : childs) {
			child.show((HtmlOut) out);
		}
	}

	@Override
	public void show(Out out) {
		show((HtmlOut) out);
	}

	@Override
	public void appendChild(Node child) {
		childs.add(child);
	}

	@Override
	public Node getChild(int i) {
		return i >= childs.size() ? null : childs.get(i);
	}

	@Override
	public Node getParent() {
		return parent;
	}

	@Override
	public List<Node> getChilds() {
		return childs;
	}

	@Override
	public void setParent(Node parent) {
		this.parent = parent;
	}

	@Override
	public String toString() {
		String a = this.getClass().getSimpleName() + "(";
		for (Node child : childs) {
			a += "\n" + child.toString();
		}
		a += ")";
		return a;
	}

	@Override
	public void clear() {
		// 删除节点
		for (Node child : childs) {
			child.clear();
		}
		// 清空链表
		childs.clear();
	}
}
